﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CCWin;
using SuperMap.Data;

namespace MDF
{
    /// <summary>
    /// 登陆检查
    /// </summary>
    public partial class LoadCheck : CCSkinMain
    {
        //配置文件名
        private string m_xmlName = "MDFUser";

        private IO_xml lc_Xml;
        /// <summary>
        /// 初始化构造
        /// </summary>
        public LoadCheck()
        {
            InitializeComponent();

            FileManage.CreateFileFolder();

            lc_Xml = new IO_xml(m_xmlName);
            
            Get_UserInfo();

            this.DialogResult = DialogResult.No;
            //界面优化
            //UI_Optimization();
        }

        ///// <summary>
        ///// 界面textbox控件大小优化
        ///// </summary>
        //private void UI_Optimization()
        //{
            //this.UserName_Text.AutoSize = false;
            //this.UserName_Text.Height = 30;
            //this.Passward_Text.AutoSize = false;
            //this.Passward_Text.Height = 30;
        //}

        /// <summary>
        /// 填充用户信息
        /// </summary>
        private void Get_UserInfo()
        {
            try
            {
                //获取有记录的用户名
                string[] names = lc_Xml.ReadChildNodesName("UserInfo");
                foreach (string name in names)
                {
                    this.UserName_Combo.Items.Add(name);
                }
                if (this.UserName_Combo.Items.Count != 0)
                {
                    //加载上一次成功登陆的信息
                    string[] lastloadnames = lc_Xml.ReadChildNodesName("LastLoadInfo");
                    foreach (string lastloadname in lastloadnames)
                    {
                        this.UserName_Combo.SelectedItem = lastloadname;
                        string password = lc_Xml.ReadValueByKey(lastloadname, "Password");
                        string check = lc_Xml.ReadValueByKey(lastloadname, "Check");
                        if (!string.IsNullOrEmpty(password))
                        {
                            //填充密码
                            this.Passward_textBox.Text = password;
                            this.RememberPassword.Checked = true;
                            if (check == "True")
                            {
                                this.AutomaticLogon.Checked = true;
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(this.UserName_Combo.Text))
                    {
                        this.UserName_Combo.SelectedIndex = 0;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 下拉列表选中，并更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserName_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //获取对应的用户数据信息
                string password = lc_Xml.ReadValueByKey(this.UserName_Combo.Text, "Password");
                string check = lc_Xml.ReadValueByKey(this.UserName_Combo.Text, "Check");
                if (!string.IsNullOrEmpty(password))
                {
                    //有密码则填充
                    this.Passward_textBox.Text = password;
                    this.RememberPassword.Checked = true;
                    if (check == "True")
                    {
                        this.AutomaticLogon.Checked = true;
                    }
                }
                else
                {
                    //没有密码则清空控件
                    this.Passward_textBox.Text = null;
                    this.RememberPassword.Checked = false;
                    this.AutomaticLogon.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        
        /// <summary>
        /// 确认登陆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if(!string.IsNullOrEmpty(UserName_Combo.Text) && !string.IsNullOrEmpty(Passward_textBox.Text))
                {
                    // 传入SuperMap Online帐号，登录自动检索可用许可
                    CloudLicense.Login(UserName_Combo.Text, Passward_textBox.Text);
                    // 连接云许可中的许可模块，验证是否云许可获取是否成功，成功返回0
                    SuperMap.Data.License lic = new SuperMap.Data.License();
                    int code = lic.Connect(65400);    //试用许可模块ID 65400
                    if (code == 0)
                    {
                        MessageBox.Show("登入成功！", "提示：");
                        //记录用户信息，是否记住密码
                        if (this.RememberPassword.Checked)
                        {
                            lc_Xml.WriteUsersInfo("UserInfo", UserName_Combo.Text, Passward_textBox.Text,AutomaticLogon.Checked.ToString());
                            lc_Xml.WriteUsersInfo("LastLoadInfo", UserName_Combo.Text, Passward_textBox.Text, AutomaticLogon.Checked.ToString());
                        }
                        else
                        {
                            lc_Xml.WriteUsersInfo("UserInfo", UserName_Combo.Text, "", AutomaticLogon.Checked.ToString());
                            lc_Xml.WriteUsersInfo("LastLoadInfo", UserName_Combo.Text, "", AutomaticLogon.Checked.ToString());
                        }
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("登入失败！错误代码：" + code.ToString()+"\n用户名或密码错误。", "提示：");
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码不能为空！", "提示：");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 自动登录
        /// </summary>
        private void OK_Login()
        {
            try
            {
                if (!string.IsNullOrEmpty(UserName_Combo.Text) && !string.IsNullOrEmpty(Passward_textBox.Text))
                {
                    // 传入SuperMap Online帐号，登录自动检索可用许可
                    CloudLicense.Login(UserName_Combo.Text, Passward_textBox.Text);
                    // 连接云许可中的许可模块，验证是否云许可获取是否成功，成功返回0
                    SuperMap.Data.License lic = new SuperMap.Data.License();
                    int code = lic.Connect(65400);    //试用许可模块ID 65400
                    if (code == 0)
                    {
                        //MessageBox.Show("登入成功！", "提示：");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("登入失败！错误代码：" + code.ToString() + "\n用户名或密码错误。", "提示：");
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码不能为空！", "提示：");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导航至注册界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Register_Label_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start("https://sso.supermap.com/phoneregister?service=http://www.supermapol.com");
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 关闭窗体时释放资源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadCheck_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.DialogResult != DialogResult.OK)
                { 
                    System.Environment.Exit(0); 
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 加载窗体后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadCheck_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.AutomaticLogon.Checked)
                {
                    //启用自动登录
                    OK_Login();

                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 是否可选自动登录，记住密码时才可选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutomaticLogon_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (!this.RememberPassword.Checked)
                {
                    this.AutomaticLogon.Checked = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消记住密码时，取消自动登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RememberPassword_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (!this.RememberPassword.Checked)
                {
                    this.AutomaticLogon.Checked = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
